Skip to content

Fix image and SVG import transform bugs#3942

Merged
Keavon merged 9 commits intomasterfrom
fix-image-import-offset
Mar 23, 2026
Merged

Fix image and SVG import transform bugs#3942
Keavon merged 9 commits intomasterfrom
fix-image-import-offset

Conversation

@Keavon
Copy link
Member

@Keavon Keavon commented Mar 23, 2026

  • Fix half-pixel offset on imported images by rounding the placement transform translation correctly (closes Opening an image to create a document has a half-pixel offset #3670)
  • Fix importing images/SVGs into offset artboards so they don't get placed with an offset from the intended position
  • Fix SVG drag-in centering to use the visual content bounds rather than the viewBox size
  • Fix SVG/image File > Open to use the declared viewBox for artboard dimensions and origin, and enable artboard clipping by default
  • Fix redundant Transform nodes when opening SVG/image files by only creating them for non-identity transforms and applying placement once to the root group
  • Fix content shift in WrapContentInArtboard to offset the parent correctly instead of the child objects

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a visual bug where imported images would appear with a half-pixel offset, particularly when added through a non-mouse-driven workflow like a file open dialog. The changes refine the coordinate transformation logic to ensure that images are always precisely aligned within the viewport, improving the accuracy of image placement.

Highlights

  • Image Import Alignment: Refactored the logic for positioning imported images, specifically addressing a half-pixel offset that occurred when images were imported without a specific mouse location.
  • Viewport Centering Calculation: Modified the calculation of center_in_viewport_layerspace and center_in_viewport to ensure precise alignment, especially when importing images via the file-open flow where mouse input is None.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a half-pixel offset issue when importing images without a specific mouse position by using DAffine2::IDENTITY to default to the document origin. The change is applied to both PasteImage and PasteSvg message handlers. The fix appears correct. My only feedback is regarding code duplication between these two handlers, which could be refactored into a helper function to improve maintainability.

@github-actions github-actions bot temporarily deployed to graphite-dev (Preview) March 23, 2026 08:15 Inactive
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 1 file

Confidence score: 3/5

  • There is a concrete regression risk in editor/src/messages/portfolio/document/document_message_handler.rs: both PasteImage and PasteSvg now fall back mouse: None to document origin, which can place pasted content far from the user’s current view.
  • This is medium severity but high confidence (6/10, 8/10), so the PR likely remains workable yet carries noticeable user-facing paste behavior risk in existing documents.
  • Pay close attention to editor/src/messages/portfolio/document/document_message_handler.rs - verify mouse: None paste placement logic so clipboard pastes stay visible in non-origin views.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="editor/src/messages/portfolio/document/document_message_handler.rs">

<violation number="1" location="editor/src/messages/portfolio/document/document_message_handler.rs:667">
P2: `PasteImage` now treats `mouse: None` as document origin for all flows, which regresses clipboard paste placement in existing documents.</violation>

<violation number="2" location="editor/src/messages/portfolio/document/document_message_handler.rs:721">
P2: `PasteSvg` applies the same `mouse: None -> origin` fallback, causing clipboard SVG paste to appear off-screen in non-origin views.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@github-actions github-actions bot temporarily deployed to graphite-dev (Preview) March 23, 2026 12:23 Inactive
@Keavon
Copy link
Member Author

Keavon commented Mar 23, 2026

@cubic-dev-ai

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Mar 23, 2026

@cubic-dev-ai

@Keavon I have started the AI code review. It will take a few minutes to complete.

@Keavon
Copy link
Member Author

Keavon commented Mar 23, 2026

/gemini review

@github-actions github-actions bot temporarily deployed to graphite-dev (Preview) March 23, 2026 21:50 Inactive
@Keavon Keavon changed the title Fix half-pixel offset on imported images Fix image and SVG import transform bugs Mar 23, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses the half-pixel offset issue for imported images and SVGs by rounding translation values. The changes also introduce significant improvements to the import process, such as respecting the SVG viewBox for artboard creation and refactoring the transform handling for SVG imports to be more robust and clear.

I've identified a couple of areas for improvement. Firstly, the rounding of translation is not applied to pasted images, which is inconsistent with the fix for SVGs and could leave the original issue partially unresolved. Secondly, there's a magic number used for node positioning in the node graph that could be replaced with a constant for better maintainability.

Overall, this is a great set of changes that improves both correctness and code quality.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 issues found across 6 files

Confidence score: 3/5

  • There is a concrete regression risk in editor/src/messages/portfolio/document/document_message_handler.rs: translation rounding is applied to SVG paths but not image transforms, so pasted/dropped images can still land on subpixel positions and appear misaligned relative to imported paths.
  • editor/src/messages/portfolio/document/document_message.rs and editor/src/messages/portfolio/portfolio_message_handler.rs both round away fractional viewBox data (IVec2 conversion and origin/size rounding), which can shift artboards and clip edge content during SVG import.
  • Given multiple medium-to-high severity import/placement issues (6–7/10) with strong confidence, this is some user-facing layout risk rather than a pure housekeeping change.
  • Pay close attention to editor/src/messages/portfolio/document/document_message_handler.rs, editor/src/messages/portfolio/document/document_message.rs, and editor/src/messages/portfolio/portfolio_message_handler.rs - rounding behavior may cause subpixel misalignment and fractional-edge clipping on import.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="editor/src/messages/portfolio/document/document_message.rs">

<violation number="1" location="editor/src/messages/portfolio/document/document_message.rs:234">
P2: Using `IVec2` for `artboard_canvas` loses fractional SVG `viewBox` values, causing subpixel offsets to be rounded away during import.</violation>
</file>

<file name="editor/src/messages/portfolio/document/document_message_handler.rs">

<violation number="1" location="editor/src/messages/portfolio/document/document_message_handler.rs:668">
P1: The image transform's translation is not rounded, but the SVG path rounds it (`placement_transform.translation = placement_transform.translation.round()`). This inconsistency means pasted/dropped images can still land at sub-pixel positions, causing the half-pixel offset this PR is fixing. Make `transform` mutable and round its translation after computing it.</violation>

<violation number="2" location="editor/src/messages/portfolio/document/document_message_handler.rs:1400">
P3: Replace the magic number `15` with `LAYER_INDENT_OFFSET + 7` to stay in sync if the indent constant ever changes. `LAYER_INDENT_OFFSET` is already imported.</violation>
</file>

<file name="editor/src/messages/portfolio/portfolio_message_handler.rs">

<violation number="1" location="editor/src/messages/portfolio/portfolio_message_handler.rs:720">
P2: Rounding viewBox origin/size to nearest integer can undersize the imported SVG artboard and clip fractional-edge content.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@Keavon Keavon force-pushed the fix-image-import-offset branch from 5248cff to 4bc6184 Compare March 23, 2026 22:49
@github-actions github-actions bot temporarily deployed to graphite-dev (Preview) March 23, 2026 22:49 Inactive
@Keavon Keavon merged commit 11b7af6 into master Mar 23, 2026
9 checks passed
@Keavon Keavon deleted the fix-image-import-offset branch March 23, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opening an image to create a document has a half-pixel offset

1 participant